package com.lyp.mvpsupermanage;

import android.app.Activity;
import android.app.Application;

import com.blankj.utilcode.util.Utils;
import com.lyp.mvpsupermanage.utils.AppUtil;


import java.util.HashSet;
import java.util.Set;

/**
 * 自定义Application，对整个应用程序作一些初始化操作
 */
public final class App extends Application {

    private static App mContext;
    private Set<Activity> allActivities;
//    private AppComponent mAppComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        AppUtil.init(this);
        mContext = this;
        //万能工具类
        Utils.init(mContext);
    }

    /**
     * 增加Activity
     *
     * @param act act
     */
    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }

    /**
     * 移除Activity
     *
     * @param act act
     */
    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }

    /**
     * 退出应用
     */
    public void exitApp() {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity act : allActivities) {
                    act.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }


    /**
     * 获取Application
     *
     * @return BiliCopyApplication
     */
    public static App getInstance() {
        return mContext;
    }


}
